package com.mkvld.appietijdenv2;

import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;

import com.mkvld.appietijdenv2.activities.ActV9;
import com.mkvld.appietijdenv2.data.Shift;
import com.mkvld.appietijdenv2.dialogs.DialCopyMoveV9;

public class DialCopyMoveV14 extends DialCopyMoveV9 {
	private NumberPicker dayPicker, weekPicker, yearPicker;
	public static void showCopy(ActV9 activity, Shift[] shifts, boolean singleWeek) {
		showDialog(activity, shifts, singleWeek, true);
	}
	
	public static void showMove(ActV9 activity, Shift[] shifts, boolean singleWeek) {
		showDialog(activity, shifts, singleWeek, false);
	}
	
	private static void showDialog(ActV9 activity, Shift[] shifts, boolean singleWeek, boolean keepOriginal) {
		DialCopyMoveV14 dial = new DialCopyMoveV14();
		Bundle args = new Bundle();
		
		args.putSerializable("shifts", shifts);
		args.putBoolean("keepOriginal", keepOriginal);
		dial.setArguments(args);
		dial.show(activity.getSupportFragmentManager(), "");
	}
	@Override
	protected void initView(View root) {
		dayPicker = (NumberPicker) root.findViewById(R.id.dcm_day);
		dayPicker.setMinValue(0);
		dayPicker.setMaxValue(6);
		dayPicker.setDisplayedValues(getResources().getStringArray(R.array.dayNames));
		dayPicker.setWrapSelectorWheel(true);
		dayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		dayPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				int newWeek = weekPicker.getValue();
				int newYear = yearPicker.getValue();
				if (oldVal == 6 && newVal == 0) {
					newWeek++;
				} else if (oldVal == 0 && newVal == 6) {
					newWeek--;
				}
				if (newWeek > 51) {
					newWeek = 0;
					newYear++;
				} else if (newWeek < 0) {
					newWeek = 51;
					newYear--;
				}
				weekPicker.setValue(newWeek);
				yearPicker.setValue(newYear);
			}
		});
		
		weekPicker = (NumberPicker) root.findViewById(R.id.dcm_week);
		weekPicker.setMinValue(0);
		weekPicker.setMaxValue(51);
		
		String[] weekNames = new String[52];
		for (int i = 0; i < weekNames.length; i++) {
			weekNames[i] = "week "+ (i + 1);
		}
		weekPicker.setDisplayedValues(weekNames);
		weekPicker.setWrapSelectorWheel(true);
		weekPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		weekPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				int newYear = yearPicker.getValue();
				if (oldVal == 51 && newVal == 0) {
					newYear++;
				} else if (oldVal == 0 && newVal == 51) {
					newYear--;
				}
				if (newYear >= 1970 && newYear <= 2050) {
					yearPicker.setValue(newYear);
				}
			}
		});
		
		yearPicker = (NumberPicker) root.findViewById(R.id.dcm_year);
		yearPicker.setMinValue(1970);
		yearPicker.setMaxValue(2050);
		//weekPicker.setDisplayedValues(getResources().getStringArray(R.array.dayNames));
		yearPicker.setWrapSelectorWheel(true);
		yearPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
	}
	
	@Override
	protected void hideDayPicker() {
		dayPicker.setVisibility(View.GONE);
	}
	
	@Override
	protected int getDayValue() {
		return dayPicker.getValue();
	}
	
	@Override
	protected int getWeekValue() {
		return weekPicker.getValue();
	}
	
	@Override
	protected int getYearValue() {
		return yearPicker.getValue();
	}
	
	
	@Override
	protected void setValues(int year, int week, int day) {
		yearPicker.setValue(year);
		weekPicker.setValue(week);
		dayPicker.setValue(day);
	}
}
